package com.example.myapplication.old;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.old.adapter.ContactAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.HotSongs;
import com.example.myapplication.entity.Song;
import com.example.myapplication.old.entity.User1;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity2 extends AppCompatActivity {
    private RecyclerView recyclerView;//声明RecyclerView
    private ContactAdapter adapter;//声明适配器
    private Context context;
    private List<User1> list = new ArrayList<>();;
    private String mp3Url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        recyclerView = findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter = new ContactAdapter();
        recyclerView.setAdapter(adapter);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        HotSongs hotSongs = (HotSongs) bundle.getSerializable("song");
        loadData(hotSongs.getId());
        initData();
        adapter.setData(list);
        adapter.setUserOnclickListener(new ContactAdapter.UserOnclickListener() {
            @Override
            public void onUserClick(User1 user1, int position) {
            }
        });
    }
    private void initData(){
        for (int i = 0; i < 25; i++) {
            User1 u1 = new User1();
            u1.setUsername("追风少年"+i+mp3Url);
            if (i%2==0){
                u1.setImg(R.drawable.a1);
            }
            else{
                u1.setImg(R.drawable.a2);
            }
            list.add(u1);
        }
    }
    private void loadData(int id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<Song> call = service.getSong(LoadingActivity.cookie,id);
        call.enqueue(new Callback<Song>() {
            @Override
            public void onResponse(Call<Song> call, Response<Song> response) {
                Song baseResponse = response.body();
                mp3Url =  baseResponse.getData().get(0).getUrl();

            }
            @Override
            public void onFailure(Call<Song> call, Throwable t) {

                t.printStackTrace();
            }
        });
    }
}